//
//  Reminder.m
//  test1
//
//  Created by Nearsoft User on 7/31/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Connection.h"
#import "Reminder.h"

@implementation Reminder

@synthesize title;
@synthesize date;
@synthesize repeat;
@synthesize complete;
@synthesize notes;
@synthesize primaryKey;

/**
 * Method name: saveToDatabase
 * Description: calls the method which edits or saves the reminder into the database and schedules its notification
 * Parameters:
 */
- (void)saveToDataBase
{
    Connection *conne = [[Connection alloc] init];

    // Sends itself to be saved or edited
    [conne saveReminderIntoDatabase:self];

    // Schedules its notification
    [self scheduleNotification];
}

/**
 * Method name: scheduleNotification
 * Description: Schedules the notificetions for the reminders
 * Parameters:
 */
- (void)scheduleNotification
{
    // Cancels the notification if already exists
    [self cancelNotification];

    // Set the notification properties
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = date;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody = title;
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.alertAction = @"View Details";

    // Set the repeat interval
    switch (repeat)
    {
                // Every Day
        case 1: localNotif.repeatInterval = NSDayCalendarUnit;
                break;
                // Every Week en the same Day
        case 2: localNotif.repeatInterval = NSWeekCalendarUnit;
                break;
                // Every Month on the same Day
        case 3: localNotif.repeatInterval = NSMonthCalendarUnit;
                break;
                //Just Once
        default: localNotif.repeatInterval = 0;
                break;
    }

    // Set the key to identify the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%i", primaryKey] forKey:@"id"];
    localNotif.userInfo = infoDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

- (UILocalNotification *)lookForNotification
{
    // Puts all the scheduled notifications on the same array
    NSArray *allNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    
    // Search for a notification with a specific key within the array
    for (UILocalNotification *singleNotification in allNotifications)
    {
        NSString *notifId = [NSString stringWithFormat:@"%@", [singleNotification.userInfo valueForKey:@"id"]];
        
        // If the notification id is equals to the reminder id cancel that notification
        if ([notifId isEqualToString:[NSString stringWithFormat:@"%i", primaryKey]])
            return singleNotification;
    }

    return nil;
}

/**
 * Method name: presentlNotification
 * Description: Presents a notification
 * Parameters: 
 */
- (void)presentNotification
{
    UILocalNotification *singleNotification = [self lookForNotification];

    if (singleNotification != nil)
        [[UIApplication sharedApplication] presentLocalNotificationNow:singleNotification];

//    // Puts all the scheduled notifications on the same array
//    NSArray *allNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
//
//    // Search for a notification with a specific key within the array
//    for (UILocalNotification *singleNotification in allNotifications)
//    {
//        NSString *notifId = [NSString stringWithFormat:@"%@", [singleNotification.userInfo valueForKey:@"id"]];
//
//        // If the notification id is equals to the reminder id present that notification
//        if ([notifId isEqualToString:[NSString stringWithFormat:@"%i", primaryKey]])
//        {
//            [[UIApplication sharedApplication] presentLocalNotificationNow:singleNotification];
//            break;
//        }
//    } 
}

/**
 * Method name: cancelNotification
 * Description: Cancels a notification
 * Parameters: 
 */
- (void)cancelNotification
{
    UILocalNotification *singleNotification = [self lookForNotification];

    while (singleNotification != nil)
    {
        [[UIApplication sharedApplication] cancelLocalNotification:singleNotification];
        singleNotification = [self lookForNotification];
    }

//    // Puts all the scheduled notifications on the same array
//    NSArray *allNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
//
//    // Search for a notification with a specific key within the array
//    for (UILocalNotification *singleNotification in allNotifications)
//    {
//        NSString *notifId = [NSString stringWithFormat:@"%@", [singleNotification.userInfo valueForKey:@"id"]];
//
//        // If the notification id is equals to the reminder id cancel that notification
//        if ([notifId isEqualToString:[NSString stringWithFormat:@"%i", primaryKey]])
//            [[UIApplication sharedApplication] cancelLocalNotification:singleNotification];
//    } 
}

@end